home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / v10n07.arc / UNIQUE.ARC / UNIQUE.ASM < prev    next >
Assembly Source File  |  1991-03-27  |  7KB  |  218 lines

  1. ;UNIQUE.COM
  2. ;
  3. ;For use in a batch file.  Replaces one of the batch file parameters (%1 - %9)
  4. ;with an 8 character unique file name (i.e. the name of a file that does not
  5. ;already exist in the current directory).  Unique does not create the file, it
  6. ;just finds a name.
  7. ;
  8. ;USAGE:   UNIQUE n   where n is the number of the parameter to be replaced
  9. ;
  10. ;An example of a batch file that uses unique follows:
  11. ;
  12. ;    ECHO OFF
  13. ;    ...
  14. ;    REM  Some batch statements
  15. ;    ...
  16. ;    UNIQUE 2
  17. ;    COPY OldData %2
  18. ;    ...
  19. ;    REM  Some more batch statements
  20. ;    ...
  21. ;    TYPE %2
  22. ;    DEL %2
  23. ;
  24. ;The command line statement calling this batch file would look something like:
  25. ;
  26. ;    DOBATCH Parameter1 12345678 AnotherParameter
  27. ;
  28. ;The parameter 12345678 is a dummy parameter that will be replaced by the
  29. ;unique file name.  It must be 8 characters long.  When the batch file gets
  30. ;to the line 'UNIQUE 2' Unique executes and replaces the second parameter on
  31. ;the command line with the unique file name.  If the second parameter does not
  32. ;exists or is not 8 characters long then Unique aborts with an error message.
  33. ;After this line has been executed %2 contains the unique name so 
  34. ;'COPY OldDate %2' copys the file OldData to the unique file found by Unique.
  35. ;
  36. ;ERRORLEVELs returned are:  1 - No parameter # specified
  37. ;                           2 - Parameter # specifed does not exists
  38. ;                           3 - Parameter # specifed is not 8 characters long
  39. ;                           4 - A unique file name could not be found
  40. cseg    segment
  41.     assume    cs:cseg, ds:cseg, es:cseg
  42.  
  43. cr    equ    0dh
  44. lf    equ    0ah
  45. beep    equ    7
  46.     
  47.     org    100h
  48. unique    proc    near
  49. start:    mov    si,80h    ;parse command line parameters
  50.     inc    si
  51. @@:    lodsb
  52.     cmp    al,0dh    ;check for end of command line
  53.     jnz    not_end
  54.     mov    dx,offset no_num    ;if at end and no number has been
  55.     mov    al,1    ;found then exit with error message
  56.     jmp    exit
  57. not_end:    cmp    al,'1'    ;is the character a number?
  58.     jl    @B    ;no
  59.     cmp    al,'9'    ;maybe
  60.     jg    @B    ;no
  61.     mov    [param_num],al    ;yes.  put in error messages for later
  62.     mov    [param_num2],al    ;use if needed.
  63.     and    al,0fh    ;change ASCII code to a 0 thru 8
  64.     dec    al
  65.     shl    al,1    ;multiple by 2 and save
  66.     push    ax
  67.     push    ds
  68. search:    mov    bx,ax        ;search backward for COMMAND MCB
  69.     mov    ax,word ptr ds:[16h]    ;get pointer to owner MCB
  70.     mov    ds,ax
  71.     cmp    ax,word ptr ds:[16h]    ;if block owns itself we have it
  72.     jnz    search        ;otherwise go try again
  73.     mov    ds,bx
  74.     mov    dx,ax
  75.     mov    ax,word ptr ds:[2ch]    ;get segment of the environment
  76.     dec    ax        ;of the first program in the
  77.     xor    si,si        ;chain after COMMAND
  78. @@:    dec    ax
  79.     mov    ds,ax        ;search backwards for previous
  80.     cmp    byte ptr [si],4dh        ;MCB
  81.     jnz    @B
  82.     cmp    [si+1],dx        ;check if owned by COMMAND
  83.     jnz    @B        ;if it is we are ready to start
  84.     pop    ds
  85.     inc    ax
  86.     mov    es,ax        ;make ES point to environment
  87.     pop    ax        ;retrieve parameter to be replaced
  88.     mov    si,0dh        ;check if it exists by get the
  89.     add    si,ax        ;pointer to it.
  90.     cmp    word ptr es:[si],0ffffh    ;if ffffh it doesn't exist
  91.     jnz    @F
  92.     mov    dx,offset no_param    ;display error message and quit
  93.     mov    al,2
  94.     jmp    exit
  95. @@:    push    word ptr es:[si]        ;save the pointer to the param.
  96.     cld
  97.     mov    di,word ptr es:[si]    ;check if it is the right length
  98.     mov    cx,9
  99.     mov    al,cr        ;if a CR (0dh) is found before 8
  100.     repnz    scasb        ;char. have been scanned it is
  101.     jnz    @F        ;too short.  If the 9th char.
  102.     jcxz    len_ok        ;isn't a CR it is too long.
  103. @@:    mov    dx,offset bad_len        ;if so show error message and quit
  104.     mov    al,3
  105.     jmp    exit
  106.  
  107. len_ok:    mov    env_seg,es        ;save pointer to environment
  108.     push    ds
  109.     pop    es
  110.     mov    ah,30h        ;get DOS version
  111.     int    21h
  112.     cmp    al,2        ;version 2.x or greater?
  113.     jge    @F
  114.     mov    dx,offset bad_dos        ;quit with error message if not
  115.     mov    ah,9        ;DOS 2.x or later
  116.     int    21h
  117.     int    20h        ;quit
  118. @@:    cmp    al,3        ;DOS 3.x?
  119.     jl    dos2        ;if not DOS 3.x we have to find
  120.     mov    dx,offset new_name    ;the unique name.  if it is 3.x or
  121.     mov    ah,5ah        ;later we let DOS create a temp
  122.     mov    cx,0        ;file for us.
  123.     int    21h
  124.     jc    no_unique        ;leave if it couldn't do it
  125.     mov    bx,ax        ;otherwise close & delete it
  126.     mov    ah,3eh        ;close file
  127.     int    21h
  128.     mov    ah,41h        ;delete file
  129.     int    21h
  130.     jmp    stuff_name
  131.  
  132. dos2:    mov    dx,offset dos2file    ;search dir. for our temp file.
  133.     mov    cx,0ffffh        ;check for file with any attribute.
  134.     mov    ah,4eh
  135.     int    21h
  136.     jc    stuff_dos2        ;if not there we have what we want.
  137.     mov    si,offset dos2file    ;otherwise we modify the name we
  138.     mov    dx,si        ;used and check again.
  139.     dec    dx
  140.     add    si,8
  141. @@:    dec    si
  142.     cmp    dx,si        ;if we've set all 8 char. to z
  143.     jne    keep_going        ;then we're finished without a
  144. no_unique:    mov    dx,offset cant_do        ;unique name
  145.     mov    al,4
  146.     jmp    exit
  147. keep_going:    cmp    byte ptr [si],'z'        ;search for the last z in the temp
  148.     jz    @B        ;file name
  149.     inc    byte ptr [si]        ;once we have it bump the char.
  150.                 ;right after it up one letter.
  151. @@:    inc    si        ;change any remaining char. to a
  152.     cmp    byte ptr [si],0        ;at the end of the name?
  153.     jz    dos2        ;loop back if so.
  154.     mov    byte ptr [si],'a'
  155.     jmp    @B
  156.  
  157. stuff_dos2:    mov    si,offset dos2file
  158.     jmp    @F
  159.  
  160. stuff_name:    mov    si,offset new_name+2
  161. @@:    mov    ax,env_seg        ;retrieve to env. segment address
  162.     mov    es,ax
  163.     pop    di
  164.     mov    cx,8        ;move the file name into the
  165.     rep    movsb        ;environment
  166.     mov    al,0dh        ;mark the end of the parameter
  167.     stosb            ;just in case
  168.     mov    al,0        ;clear the ERRORLEVEL and
  169.     mov    dx,0        ;the error message pointer
  170. exit:    cmp    dx,0        ;check if we need to display
  171.     jz    @F        ;anything
  172.     push    ax
  173.     mov    ah,9h
  174.     int    21h
  175.     mov    dx,offset help_msg    ;display help
  176.     mov    ah,9
  177.     int    21h
  178.     pop    ax
  179. @@:    mov    ah,4ch        ;leave
  180.     int    21h
  181.  
  182. new_name    db    '.\'
  183.     db    9 dup (0)
  184. env_seg    dw    0
  185. no_param    db    beep,cr,lf,'Dummy parameter %'
  186. param_num    db    '0 not present on command line.',cr,lf,'$'
  187. bad_len    db    beep,cr,lf,'Parameter %'
  188. param_num2    db    '0 is not 8 characters long.',cr,lf,'$'
  189. no_num    db    beep,cr,lf,'No parameter number was specified.',cr,lf,'$'
  190. bad_dos    db    beep,cr,lf,'DOS Version 2.0 or greater required.',cr,lf,'$'
  191. cant_do    db    beep,cr,lf,"Can't create unique file.",cr,lf,'$'
  192. dos2file    db    'aaaaaaaa',0
  193. help_msg    db    cr,lf
  194.     db    'UNIQUE.COM   Version 1.0    by Ken Hipple (CIS: 74076,2331)',cr,lf
  195.     db    'FREE software  Copyright 1989  All Right Reserved  No warranty implied or given',cr,lf
  196.     db    lf
  197.     db    'For use in a batch file.  Replaces one of the batch file parameters (%1 - %9)',cr,lf
  198.     db    'with an 8 char. unique file name.',cr,lf
  199.     db    lf
  200.     db    'USAGE:   UNIQUE n   where n is the number of the parameter to be replaced',cr,lf
  201.     db    lf
  202.     db    'Example batch file            Example command line',cr,lf
  203.     db    lf
  204.     db    '  ECHO OFF                DOBATCH Param1 12345678 Param2',cr,lf
  205.     db    '  UNIQUE 2',cr,lf
  206.     db    '  COPY OldData %2',cr,lf
  207.     db    '  TYPE %2',cr,lf
  208.     db    '  DEL %2',cr,lf
  209.     db    lf
  210.     db    'The parameter 12345678 is a dummy parameter that will be replaced by the',cr,lf
  211.     db    'unique file name.  It must be 8 characters long.  ERRORLEVELs returned are:',cr,lf
  212.     db    '1-No parameter # specified                2-Parameter specifed does not exist',cr,lf
  213.     db    '3-Parameter specifed not 8 char. long     4-A unique name could not be found',cr,lf,'$'
  214.  
  215. unique    endp
  216. cseg    ends
  217.     end    start
  218.